using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;

namespace ClientTools.Server.Webservices
{
    /// <summary>
    /// Summary description for ClientToolsMediaService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class ClientToolsMediaService : ClientToolsBaseService
    {

        private umbraco.BusinessLogic.User _umbracoUser;

        /// <summary>
        /// Authenticates the specified user.
        /// </summary>
        /// <param name="UserName">Name of the user.</param>
        /// <param name="Password">The password.</param>
        /// <returns></returns>
        private void Authenticate(string userName, string password)
        {
            this._umbracoUser = this.Authenticate(userName, password, ServiceTypeEnum.MediaService);
        }

        [WebMethod]
        public Library.Media.MediaCollection GetRootMedias(string userName, string password, bool recursive)
        {
            this.Authenticate(userName, password);
            if (this._umbracoUser.StartMediaId == -1)
            {
                return new Library.Media.MediaManager().GetRootMedias(recursive);
            }
            else
            {
                return new Library.Media.MediaManager().GetMediasFromNode(this._umbracoUser.StartMediaId, recursive);
            }
        }

        [WebMethod]
        public Library.Media.MediaCollection GetMedias(string userName, string password, int parentMediaId, bool recursive)
        {
            this.Authenticate(userName, password);
            if (this._umbracoUser.StartMediaId == -1)
            {
                return new Library.Media.MediaManager().GetRootMedias(recursive);
            }
            else
            {
                return new Library.Media.MediaManager().GetMediasFromNode(this._umbracoUser.StartMediaId, recursive);
            }
        }

        [WebMethod]
        public bool DeleteMedia(string userName, string password, int mediaId)
        {
            this.Authenticate(userName, password);
            return new Library.Media.MediaManager().DeleteMedia(mediaId);
        }

        [WebMethod]
        public Library.Media.Media CreateImage(string userName, string password, int parentMediaId, string fileName, byte[] content)
        {
            this.Authenticate(userName, password);
            return new Library.Media.MediaManager().CreateNewImage(this._umbracoUser, parentMediaId, fileName, content);
        }

        [WebMethod]
        public Library.Media.Media CreateFile(string userName, string password, int parentMediaId, string fileName, byte[] content)
        {
            this.Authenticate(userName, password);
            return new Library.Media.MediaManager().CreateNewFile(this._umbracoUser, parentMediaId, fileName, content);
        }

        [WebMethod]
        public Library.Media.Media CreateFolder(string userName, string password, int parentMediaId, string folderName)
        {
            this.Authenticate(userName, password);
            return new Library.Media.MediaManager().CreateNewFolder(this._umbracoUser, parentMediaId, folderName);
        }
    }
}
